package com.phuongtn2.karaoke.database;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.phuongtn2.karaoke.database.dto.RoomDto;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class DatabaseHelper extends SQLiteOpenHelper {
	public static final int newVersion= 1;
	public static final String databaseName = "karaoke";
	public static final String roomTable = "room";
	public static final String roomId = "roomId";
	public static final String roomName = "roomName";
	public static final String roomRemark="roomRemark";
	public static final String roomStatus = "roomStatus";

	public DatabaseHelper(Context context) {
		super(context, DatabaseHelper.databaseName, null, newVersion);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase database) {
		// TODO Auto-generated method stub
		String query = "CREATE TABLE " + roomTable + " (" + roomId
				+ " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " + roomName
				+ " TEXT NOT NULL, " + roomRemark
				+ " TEXT, " + roomStatus + " TEXT )";
		database.execSQL(query);

	}

	public void insertRoom(RoomDto roomDto) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues cv = new ContentValues();
		// cv.put(roomId, 1);
		cv.put(roomName, roomDto.getRoomName());
		cv.put(roomRemark, roomDto.getRoomRemark());
		cv.put(roomStatus, roomDto.getRoomStatus());
		db.insert(roomTable, null, cv);
		db.close();
	}

	

	public void updateRoom(RoomDto roomDto) {
		SQLiteDatabase database = this.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put(roomName, roomDto.getRoomName());
		cv.put(roomRemark, roomDto.getRoomRemark());
		cv.put(roomStatus, roomDto.getRoomStatus());
		database.update(roomTable, cv, roomId + "=" + roomDto.getRoomId(), null);
		close();
	}
	public List<RoomDto> updateStatus(RoomDto roomDto){
		List<RoomDto> dtos = new ArrayList<RoomDto>();
		dtos = getAllRoom();
		SQLiteDatabase database = this.getWritableDatabase();
		ContentValues cv = new ContentValues();
		for (Iterator<RoomDto> iterator = dtos.iterator(); iterator.hasNext();) {
			RoomDto dt = (RoomDto) iterator.next();
			if(roomDto.getRoomId()!=dt.getRoomId()){
				cv.put(roomStatus, "deactive");
			}else{
				cv.put(roomStatus, "active");
			}
			database.update(roomTable, cv, roomId + "=" + dt.getRoomId(), null);
		}
		close();
		return  getAllRoom();
	}
	public void deleteRoom(RoomDto roomDto) {
		SQLiteDatabase database = this.getWritableDatabase();
		database.delete(roomTable, roomId + "=" + roomDto.getRoomId(), null);
		close();
	}
	private RoomDto cursorToRoomDto(Cursor cursor) {
		RoomDto roomDto = new RoomDto();
		roomDto.setRoomId(cursor.getInt(0));
		roomDto.setRoomName(cursor.getString(1));
		roomDto.setRoomStatus(cursor.getString(2));
		return roomDto;
	}
	public List<RoomDto> getAllRoom() {
		List<RoomDto> dtos = new ArrayList<RoomDto>();
		SQLiteDatabase database = this.getReadableDatabase();
		String query = "SELECT  * FROM " + DatabaseHelper.roomTable;
		Cursor cursor = database.rawQuery(query, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			RoomDto roomDto = cursorToRoomDto(cursor);
			dtos.add(roomDto);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return dtos;
	}

	// @Override
	// public void onOpen(SQLiteDatabase database) {
	// database = DatabaseHelper.getWritableDatabase();
	// super.onOpen(db);
	// if (!db.isReadOnly()) {
	// Enable foreign key constraints
	// db.execSQL("PRAGMA foreign_keys=ON;");
	// }
	// }

	@Override
	public void onUpgrade(SQLiteDatabase database, int oldVersion,
			int newVersion) {
		// TODO Auto-generated method stub
		Log.w(DatabaseHelper.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");
		database.execSQL("DROP TABLE IF EXISTS " + roomTable);
		onCreate(database);
	}

}
